Hi, I use those forums for about 2 weeks when I first start codding in C but I think this is the time to register .
I have a problem with my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char replacerand(char *str);
int main() {
char str[] = "f hgfgh #RANDOM(David Lai|9123 4567|Dav|valu e1|val ue2|valu e3|value4|value5)#\n"
"fdslk gfdlkgfd #RANDOM(mom1|fgds|ggg|gfdgfd)# jkfgjk hgkj\n";
replacerand(str);
}
char replacerand(char *str) {
int i;
int count = 0;
char string33[200];
char string34[200];
sscanf(str, "#RANDOM(%s%[^)]", string33, string34);
sprintf(str, "%s%s", string33, string34);
printf("\n\n\n--- %s ---\n", str);
const char delims[] = "|";
char *result = NULL;
char **store = NULL;
char **tmp = NULL;
char *name = NULL;
char *telephone = NULL;
char *nickname = NULL;
int randomnum;
int totalval;
result = strtok(str, delims);
while (result != NULL) {
free(tmp);
tmp = malloc(count * sizeof(char *));
for (i=0; i<count; i++) {
tmp[i] = store[i];
}
free(store);
store = malloc((count + 1) * sizeof(char *));
for (i=0; i<count; i++) {
store[i] = tmp[i];
}
store[count] = result;
//printf("%s", store[1]);
count++;
//printf(".%s\n", result);
result = strtok(NULL, delims);
}
int f;
srand(time(NULL));
//srand(time(NULL));
randomnum = rand() % count;
printf("%s %d %d\n", store[randomnum], count, randomnum);
free(tmp);
free(store);
return 0;
}
First I'm trying to transform every #RANDOM(whatever) into arr to a random value delimited by a "|".
I'm just not ableto transform them all since the sscanf() do not seems to understand what I'm trying to do.
Anyone ?